﻿
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

using Gift.DAL;
using Gift.Model;


    public partial class admin_company_CompanyMoneyList : BaseAdmin
    {
        readonly dalCompany theCompany = new dalCompany();

        string ParamentType
        {
            get {
                return Request.QueryString["type"];
            }
        }


        public void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (ParamentType == "publish")
                {
                    this.tdLevelName.Visible = false;
                    this.tdLevelValue.Visible = false;
                    this.tdCompany.Visible = false;
                    this.tdCompanySelect.Visible = false;
                    base.ValidateLoginRule(Gift.Model.ENUM.ERule.PublishCompanyMoneyList);
                }
                else  if (base.CompanyId == null )
                {
                    base.ValidateLoginRule(Gift.Model.ENUM.ERule.CompanyMoneyList);
                }
                else
                {
                    this.tdCompany.Visible = false;
                    this.tdCompanySelect.Visible = false;
                }

                this.BindCompanyLevel();
                this.BindCompanyList();
                this.BindGridView();
            }
        }

        void BindGridView()
        {
            DataTable dt;
            string sWhere = " 1=1 ";

            if (ParamentType == "publish")
            {
                sWhere = " LevelId= 1";
            }
            else  if (this.ddlLevel.SelectedItem.Value != "")
            {
                sWhere = " LevelId= " + this.ddlLevel.SelectedItem.Value;
            }
            int iCompanyId = 0;

            dt = theCompany.GetMoneyList(sWhere).Tables[0];//.GetList("", "");
            if (base.CompanyId != null)
            {
                if (!string.IsNullOrEmpty(this.ddlCompany.SelectedItem.Value))
                {
                    iCompanyId = int.Parse(this.ddlCompany.SelectedItem.Value);

                }
                else
                {
                    iCompanyId = int.Parse(base.CompanyId);
                }
                dt = GetAllCompany(dt, null, iCompanyId, false);
            }
            else
            {
                if (!string.IsNullOrEmpty(this.ddlCompany.SelectedItem.Value))
                {
                    iCompanyId = int.Parse(this.ddlCompany.SelectedItem.Value);

                }
                else
                {
                    iCompanyId = 0;
                }
                dt = GetAllCompany(dt, null, iCompanyId, true);
            }

            if (dt != null)
            {
                if (this.txtKeywords.Text.Trim() != "")
                {
                    dt.DefaultView.RowFilter = string.Format(" (CompanyName like '%{0}%' or CompanyCode = '{0}' or CompanyShortName like '%{0}%')", this.txtKeywords.Text.Trim());
                }
                dt.DefaultView.Sort = " CompanyName Asc ";
                this.GridView1.DataSource = dt.DefaultView;
                this.GridView1.DataBind();

                this.LblCurrentIndex.Text = "当前第 " + ((this.GridView1.PageIndex + 1)).ToString() + "页";
                this.LblPageCount.Text = "共 " + this.GridView1.PageCount.ToString() + "页";
                this.LblRecordCount.Text = "总计 " + dt.Rows.Count.ToString() + "个记录";
                if (dt.Rows.Count == 0)
                {
                    this.btnFirst.Visible = false;
                    this.btnPrev.Visible = false;
                    this.btnNext.Visible = false;
                    this.btnLast.Visible = false;
                    this.LblCurrentIndex.Visible = false;
                    this.LblPageCount.Visible = false;
                    this.LblRecordCount.Visible = false;
                }
                else if (this.GridView1.PageCount == 1)
                {
                    this.btnFirst.Visible = false;
                    this.btnPrev.Visible = false;
                    this.btnNext.Visible = false;
                    this.btnLast.Visible = false;
                }
                this.btnFirst.CommandName = "1";
                this.btnPrev.CommandName = (this.GridView1.PageIndex == 0) ? "1" : this.GridView1.PageIndex.ToString();
                this.btnNext.CommandName = (this.GridView1.PageCount == 1) ? this.GridView1.PageCount.ToString() : ((this.GridView1.PageIndex + 2)).ToString();
                this.btnLast.CommandName = this.GridView1.PageCount.ToString();
                this.ddlCurrentPage.Items.Clear();
                if (this.GridView1.PageCount > 0)
                {
                    for (int i = 1; i <= this.GridView1.PageCount; i++)
                    {
                        this.ddlCurrentPage.Items.Add(i.ToString());
                    }
                    this.ddlCurrentPage.SelectedIndex = this.GridView1.PageIndex;
                }
            }
        }

 

        protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
        {

        }

        protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
        {

        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            string sIds = "";
            for (int i = 0; i <= (this.GridView1.Rows.Count - 1); i++)
            {
                CheckBox box = (CheckBox)this.GridView1.Rows[i].FindControl("CheckBox1");
                if (box.Checked)
                {
                    sIds = sIds + this.GridView1.DataKeys[i].Value + ",";
                }
            }
            if (sIds.Length > 0)
            {
                sIds = sIds.Remove(sIds.Length - 1);

                theCompany.DeleteMany(sIds);
                base.Response.Write("<script>alert('批量删除成功!')</script>");
                this.BindGridView();
            }

        }

        protected void PagerButtonClick(object sender, EventArgs e)
        {
            this.GridView1.PageIndex = Convert.ToInt32(((LinkButton)sender).CommandName) - 1;
            this.BindGridView();
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.GridView1.PageIndex = this.ddlCurrentPage.SelectedIndex;
            this.BindGridView();
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if ((e.Row.RowType == DataControlRowType.Header) || (e.Row.RowType == DataControlRowType.DataRow))
            {
                    if (e.Row.RowType == DataControlRowType.DataRow)
                    {
                        e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=\"" + e.Row.Style["BACKGROUND-COLOR"] + "\"");
                        e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor=\"#efefef\"");

                        if (GridView1.DataKeys[e.Row.RowIndex].Value.ToString() == base.CompanyId || !base.GetValidateLoginRule(Gift.Model.ENUM.ERule.CompanyMoney))
                        {
                             e.Row.Cells[7].Controls[1].Visible = false;
                             e.Row.Cells[7].Controls[3].Visible = false;
                        }
                    }
            }
        }


        

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {

        }

        void BindCompanyList()
        {
            DataTable dt;
            string sWhere = " 1=1 ";
            dt = theCompany.GetList(sWhere).Tables[0];
            if (base.CompanyId != null)
            {
                dt = GetAllCompany(dt, null, int.Parse(base.CompanyId), false);
            }
            else
            {
                dt = GetAllCompany(dt, null, 0, true);
            }
            this.ddlCompany.DataTextField = "Name";
            this.ddlCompany.DataValueField = "Id";
            this.ddlCompany.DataSource = dt;
            this.ddlCompany.DataBind();
            this.ddlCompany.Items.Insert(0, new ListItem("请选择商家...", ""));
        }

        void BindCompanyLevel()
        {
            DataSet ds = theCompany.GetCompanyLevelList();
            if (base.CompanyId != null)
            {
                ds.Tables[0].DefaultView.RowFilter = " Id >= " + base.LevelId;
            }
            this.ddlLevel.DataTextField = "LevelName";
            this.ddlLevel.DataValueField = "Id";
            this.ddlLevel.DataSource = ds.Tables[0].DefaultView;
            this.ddlLevel.DataBind();
            this.ddlLevel.Items.Insert(0, new ListItem("请选择商家级别...", ""));
        }

        protected void ibtnSearch_Click(object sender, ImageClickEventArgs e)
        {
            this.BindGridView();
        }
    }

